$(function () {
    var timer = null;
    initHtmlOfShopCart();

    $('#settlement').tap(function () {
        if ($('#numbers').text() <= 0) {
            return false;
        }
        storageIds();
        window.location.href = "confirm_order.html";
    });
    $('#chooseAll').tap(function () {
        $('.check', '#product').prop('checked', this.checked);
        getCheckLength();
        allPrice();
    });
    $('#product')
        .delegate('button.plus', 'tap', function () {
            var _vm = $(this);
            var number = _vm.prev('.number');
            var num = number.val();
            var id = _vm.parents('.product').attr('data-id');
            num++;
            number.val(num);
            updateNumberById(id, num);
            allPrice();
        })
        .delegate('button.reduce', 'tap', function () {
            var _vm = $(this);
            var number = _vm.next('.number');
            var num = number.val();
            var id = _vm.parents('.product').attr('data-id');
            if (num > 1) {
                num--;
                number.val(num);
            }
            updateNumberById(id, num);
            allPrice();
        })
        .delegate('input.number', 'keyup', function () {
            var _vm = $(this);
            var num = _vm.val();
            var id = _vm.parents('.product').attr('data-id');
            _vm.val(num.replace(/^0+|[^\d]/g, ''));
            updateNumberById(id, num);
            allPrice();
        })
        .delegate('input.number', 'blur', function () {
            var _vm = $(this);
            var num = _vm.val();
            var id = _vm.parents('.product').attr('data-id');
            if (!num) {
                _vm.val(1);
            }
            updateNumberById(id, num);
            allPrice();
        })
        .delegate('input.check', 'tap', function () {
            var isCheckAll = $("#product input.check").not("input:checked").size() <= 0;
            $("#chooseAll").prop("checked", isCheckAll);
            allPrice();
            getCheckLength();
        });

    function initHtmlOfShopCart() {
        qingQiuPhp('get', 'admin/shop_cart.php', {getType: 'getShopCartList'}, function (res) {
            var str = '';
            for (var i = 0, n = res.length; i < n; i++) {
                str +=
                    '<div class="product" data-id="' + res[i].shopId + '">' +
                    '<div class="left">' +
                    '<input name="check" class="check" type="checkbox">' +
                    '<img src="' + res[i].imgSrc + '" alt="">' +
                    '</div>' +
                    '<div class="right">' +
                    '<h6>' + res[i].title + '</h6>' +
                    '<p>颜色：（<span>' + res[i].scolor + '</span>）</p>' +
                    '<strong>￥<span class="shopPrice">' + Number(res[i].price).toFixed(2) + '</span></strong>' +
                    '<div class="num">' +
                    '<button class="reduce">-</button>' +
                    '<input type="tel" class="number" value="' + res[i].num + '">' +
                    '<button class="plus">+</button>' +
                    '</div>' +
                    '</div>' +
                    '</div>';
            }
            $('#product').html(str);
        });
    }

    function getCheckLength() {
        var len = $('.product input:checked', '#product').length;
        $('#numbers').text(len);
    }

    function allPrice() {
        var product = $('.product', '#product');
        var total = 0;
        for (var i = 0, n = product.length; i < n; i++) {
            var inputs = product.eq(i).find('.check').prop('checked');
            var shopPrice = product.eq(i).find('.shopPrice').text();
            var number = product.eq(i).find('.number').val();
            if (inputs) {
                total += shopPrice * number;
            }
        }
        $('#ptoTotal').text(total.toFixed(2));
    }

    function updateNumberById(id, num) {
        var option = {
            type: 'post',
            data: {
                postType: 'changeNum',
                id: id,
                num: num
            },
            url: 'admin/shop_cart.php'
        };
        if (timer) {
            clearTimeout(timer);
        }
        timer = setTimeout(function () {
            requestEvent(option, function (res) {
                console.log(res);
            })
        }, 500);
    }

    function storageIds() {
        var ids = [];
        $('#product input.check:checked').parents('.product').each(function () {
            ids.push($(this).attr('data-id'));
        });
        LsySession.setItem('checkItems', ids);
    }
});

